
var User = Class({
	user_id:false,
	
	initialize:function() {
		BC.addListener(this);
		
		this.user_id = false;
		this.u_type = false;
		
		var _parent = this;
		j.request('user_getdata','',function (response) { 
			var result = JSON.decode(response);
			if(typeof result == 'object') {
				for(e in result) {
					_parent[e] = result[e];
				}
			}
			
			if(!_parent.user_id) {
				_parent.loginForm();
				BC.broadcastMessage('user.logout');
				
			} else {
				BC.broadcastMessage('user.getdata');
			}
		});
	},
	
	edit:function() {
		var floater = new Modal({
			height:320,
			content:'<div class="loading">loading user data</div>',
			windowtitle:'Settings'
		});

		$('floating_content').innerHTML = templates.useredit.process(this);
	},
	
	save:function() {
		$('response').innerHTML = '';
		packet = {};
		
		// check if password is set
		if($('p_password_new').value != '') {
			if ($('p_password_new').value != $('p_password_retype').value) {
				$('response').innerHTML = 'The new password and the retyped password dont match.';
				return;
			} else {
				packet.password = hex_md5($('p_password_new').value+"Das weiss ich nicht");
			}
		}
		
		if($('p_login').value === '') {
			$('response').innerHTML = 'Please enter a username containing at least 6 characters and 2 numbers.';
			return;
		}
		if($('p_name').value === '') {
			$('response').innerHTML = 'Please enter your name.';
			return;
		}
		
		packet.name = $('p_name').value;
		packet.email = $('p_email').value;
		packet.login = $('p_login').value;
		
		var _this = this;
		j.request('user_update',packet,function(obj) {
			if(!obj) {
				_this.u_name = packet.name;
				_this.u_email = packet.email;
				_this.u_login = packet.login;
				
				$('response').innerHTML = 'saved.';
				BC.broadcastMessage('user.updated');
			}
		});
	},
	
	/**
	 * login function expects to be called on the login template and to find 
	 * the form named "login_form" containing fields "u_login" "u_password" and "loginbutton"
	 * as well as the container "loginresult" to display the login result.
	 * 
	 */
	 login:function () {
		$('loginbutton').disabled = true;
		$('u_login').disabled = true;
		$('u_password').disabled = true;
		$('loginresult').innerHTML = 'logging in';
	    
	    var _this = this;
	    
		j.request('login', {
				'u_login':$('u_login').value,
				'u_password': hex_md5($('u_password').value+"Das weiss ich nicht")
			},
			function(obj) {
				var response = JSON.decode(obj);
				if(response.result == "success") {
					$('contentbox').removeEvent(window.ie ? 'keydown' : 'keypress');
					$('loginresult').innerHTML = "success...";
					
					for(e in response.userdata) {
						_this[e] = response.userdata[e];
					}
					
					BC.broadcastMessage('user.login');
					
				} else {
					$('loginbutton').disabled = false;
					$('u_login').disabled = false;
					$('u_password').disabled = false;
					$('u_password').value = '';
					$('loginresult').innerHTML = response.reason;
				}
			});
	 },
	  
	/**
	 * logout function
	 * 
	 * This funktion resets all global objects and variables
	 */
	 logout:function() {
		j.request('logout','',function() { user = new User(); });
		this.floater.close();
		this.loginForm();
	},
	
	loginForm:function () {
		$('contentbox').innerHTML = templates.login;
		$('u_login').focus();
		$('contentbox').getElement('#login_form').addEvent('submit', function (ev) {
			ev = new Event(ev).stop();
			user.login();
		});
	},
	
	/**
	 * brings up the logout floating window
	 */
	logout_show:function () {
		this.floater = new Modal({
			windowtitle:'Logout?',
			height:110,
			width:250,
			content:'<div class="button" onclick="user.logout();"> yes </div> <div class="button" onclick="user.floater.close();"> no </div>'
		});
	}
});
